<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Imprest extends Model
{
    use HasFactory;

    protected $fillable = ['user_id','role_id','project_id','apply_date','purpose','nature','pay_type','receive_type','apply_price','back_borrow','audit_price','price','pay_company','bank_charges','name','bank','bank_number','bank_no','remarks'];

    const POSTONLY = ['role_id','user_id','project_id','apply_date','purpose','nature','pay_type','receive_type','apply_price','back_borrow','audit_price','price','pay_company','bank_charges','name','bank','bank_number','bank_no','remarks'];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function role()
    {
        return $this->hasOne(Role::class,'id','role_id')->select('id','name');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->orderBy('id','desc')->where('model','Imprest');
    }

    public function applicationApproved()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','Imprest')->where('status','approved')->orderBy('id','desc');
    }


}
